<?php


namespace app\api\controller;


use app\common\model\AdminModel;
use app\common\model\BusOrderModel;
use app\common\model\GoodslistModel;
use app\common\model\OrderitemMoel;
use think\Request;

class Userindex extends  Base
{
    ////获取
    public function index(Request $request){
        $user = new AdminModel();
        $goods = new GoodslistModel();
        $array = array();
        $search_key = $request->param('key');
        ///判断是否有关键字,如果有关键字，先进行一次筛选
//        if(isset($key) && !empty($key)){
//            $user = $user->where("nickname", 'like', '%'.$key.'%');     /// 模糊搜索
//        }

        ///未禁用
        $userinfo = $user->where('status', 0)->where('role', 2)->order("is_recommended desc")->select();
        foreach ( $userinfo as $key=>$value){
            $array1 = [];
            $array1['username'] = $value['username'];
            $array1['nickname'] = $value['nickname'];
            $array1['busid'] = $value['id'];
            $array1['addr'] = $value['addr'];
            $array1['telephone'] = $value['telephone'];
            $array1['brief'] = $value['brief'];
            $array1['order_finished_number'] = $value['order_finished_number'];
            $array1['img'] = $value['img'];

            $temparray = array();
            ////添加商家对应的物品
            $goods = new GoodslistModel();

            $array1['recommended_count'] = $goods->where('busid',$array1['busid'])->where('status',1)->where('is_recommended',1)->count();
            /*==== $goodsinfo = $goods->where('busid', $array1['busid'])->select();=====*/
            if($search_key!="") {
                $goods=$goods->where('goods_name','like','%'.$search_key.'%');
            }
            $goodsinfo = $goods->where('busid', $array1['busid'])->where('status',1)->order(['is_recommended'=>'desc','sellcount'=>'desc'])->select();
//            echo $goods->getLastSql();
//            echo "<br>";
            foreach ($goodsinfo as $key =>$value){
                $array2 = [];
                $array2['goods_name'] = $value['goods_name'];
                $array2['goods_id'] = $value['id'];
                $array2['goods_price'] = $value['price'];
                $array2['goods_count'] = $value['count'];
                $array2['goods_img'] = $value['img'];
                $array2['is_recommended']=$value['is_recommended'];
                $array2['sellcount'] = $value['sellcount'];
                $array2['brief'] = $value['brief'];
                $array2['score_people'] = $value['people_score_number'];
                if($value['people_score_number']==0)$array2['score']='未打分';
                else $array2['score']=number_format($value['total_score']/$value['people_score_number'],2);
                $temparray[] = $array2;
            }
            $array1['goods'] = $temparray;
            if(count($goodsinfo)>0)
                $array[] = $array1;

        }
        return json(["shop_goods_list"=>$array, 'code'=>1, 'msg'=>'获取信息成功']);
    }

    public function busindex(Request $request)
    {
        $busid=$request->param('busid');
        $array = array();
        $search_key = $request->param('key');
        $kind = $request->param('kind');

        $busorder=new BusOrderModel();

        if($kind==0 && $search_key!=""){
            $busorder=$busorder->where("orderid", 'like', '%'.$search_key.'%');     /// 按订单模糊搜索
        }

        $orderinfo = $busorder->where('busid', $busid)->order(['create_time'=>'desc'])->select();
        $array = array();

        foreach ( $orderinfo as $key=>$value){
            $array1 = [];
            $array1['userid'] = $value['userid'];
            $array1['totalAmout'] = $value['totalAmout'];
            $array1['totalNum'] = $value['totalNum'];
            $array1['create_time'] = $value['create_time'];
            $array1['state'] = $value['state'];
            $array1['address'] = $value['address'];

            if($array1['state']==1)
                $array1['order_cancel']=true;
            else
                $array1['order_cancel']=false;

            $array1['orderid'] = $value['orderid'];
            $array1['busid'] = $value['busid'];
            $array1['id'] = $value['id'];
            $temparray = array();
            ////添加商家对应的物品
            $orderitem = new OrderitemMoel();

            $user=new AdminModel();
            $userinfo=$user->where('id',$array1['userid'])->find();
            $array1['nickname']=$userinfo['nickname'];
            $array1['telephone']=$userinfo['telephone'];


            if($kind==1&&$search_key!=""){
                $orderitem=$orderitem->where("goods_name", 'like', '%'.$search_key.'%');     /// 按菜品模糊搜索
            }


            $orderiteminfo = $orderitem->where('orderid', $array1['orderid'])->where('busorderid',$array1['id'])
                ->where('status', 1)->select();

            foreach ($orderiteminfo as $key =>$inner_value){
                $array2 = [];
                $array2['shop_id'] = $inner_value['shop_id'];
                $array2['shop_name'] = $inner_value['shop_name'];
                $array2['goods_id'] = $inner_value['goods_id'];
                $array2['goods_name'] = $inner_value['goods_name'];
                $array2['goods_img'] = $inner_value['goods_img'];
                $array2['price'] = $inner_value['price'];
                $array2['count'] = $inner_value['count'];
                $array2['score_state'] = $inner_value['score_state'];
                $temparray[] = $array2;
            }
            $array1['goods'] = $temparray;
            if($kind==0 || !$orderiteminfo->isEmpty())
                $array[] = $array1;
        }
        return json(['code'=>1,'msg'=>'查询成功','order_list'=>$array]);
    }
}